﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace ExFlash.Records
{
    [TypeConverter(typeof(MorphFillStyleConverter))]
    public class MorphFillStyle : ISwfSerializable
    {
        public FillStyleType FillStyleType
        {
            get;
            protected set;
        }

        public RGBARecord StartColor
        {
            get;
            protected set;
        }

        public RGBARecord EndColor
        {
            get;
            protected set;
        }

        public MatrixRecord StartGradientMatrix
        {
            get;
            protected set;
        }

        public MatrixRecord EndGradientMatrix
        {
            get;
            protected set;
        }

        public MorphGradient Gradient
        {
            get;
            protected set;
        }

        public ushort BitmapId
        {
            get;
            protected set;
        }

        public MatrixRecord StartBitmapMatrix
        {
            get;
            protected set;
        }

        public MatrixRecord EndBitmapMatrix
        {
            get;
            protected set;
        }

        #region ISwfSerializable Members

        public void SwfDeserialize(BinaryBitReader reader)
        {
            FillStyleType = (FillStyleType)reader.ReadByte();

            switch (FillStyleType)
            {
                case FillStyleType.SolidFill:
                    {
                        StartColor = new RGBARecord();
                        StartColor.SwfDeserialize(reader);

                        EndColor = new RGBARecord();
                        EndColor.SwfDeserialize(reader);
                        break;
                    }
                case FillStyleType.LinearGradientFill:
                case FillStyleType.RadialGradientFill:
                    {
                        StartGradientMatrix = new MatrixRecord();
                        StartGradientMatrix.SwfDeserialize(reader);

                        EndGradientMatrix = new MatrixRecord();
                        EndGradientMatrix.SwfDeserialize(reader);

                        Gradient = new MorphGradient();
                        Gradient.SwfDeserialize(reader);
                        break;
                    }
                case FillStyleType.RepeatingBitmapFill:
                case FillStyleType.ClippedBitmapFill:
                case FillStyleType.NonSmoothedRepeatingBitmap:
                case FillStyleType.NonSmoothedClippedBitmap:
                    {
                        BitmapId = reader.ReadUInt16();

                        StartBitmapMatrix = new MatrixRecord();
                        StartBitmapMatrix.SwfDeserialize(reader);

                        EndBitmapMatrix = new MatrixRecord();
                        EndBitmapMatrix.SwfDeserialize(reader);
                        break;
                    }
                default:
                    {
                        throw new SerializationException("Error when deserializing MorphFillStyle - Unknown FillStyleType.");
                    }
            }
            
        }

        public void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class MorphFillStyleConverter : ExpandableObjectConverter
    {

    }
}
